<?php

Class FollowsController extends AppController {

    public $name = 'Follows';

    public function edit($id) {
        $this->layout = "ajax";
        if (empty($this->data)) {
            $this->data = $this->Follow->find('first', array('conditions' => array('Follow.id' => $id), 'fields' => array('Follow.*')));
        } else {
            if ($this->Follow->save($this->data)) {
                $this->Session->setFlash('Registro editado correctamente');
                $this->redirect(array('controller' => 'Follows', 'action' => 'index'));
            } else {
                $this->Session->setFlash('Error editando datos');
            }
        }
    }

    public function add($proyect_id) {
        $this->layout = "ajax";
        if ($evaluacion = $this->Follow->FinalEvaluation->find('first', array('conditions' => array('FinalEvaluation.proyect_id' => $proyect_id), 'fields' => array('FinalEvaluation.id'), 'order' => array('FinalEvaluation.id DESC')))) {
            $this->request->data['Follow']['final_evaluation_id'] = $evaluacion['FinalEvaluation']['id'];
            if ($this->Follow->save($this->request->data)) {
                $this->Session->setFlash('Registro creado correctamente', 'flash_custom');
                $this->redirect(array('controller' => 'Follows', 'action' => 'index'));
            }
        } else {
            $this->Session->setFlash('Este proyecto no ha sido evaluado');
            $this->redirect(array('controller' => 'Follows', 'action' => 'index'));
        }
    }

    public function index() {
        $this->layout = "ajax";
        $proyect_id = $this->Session->read('proy_id');
        $this->set('proyect_id', $proyect_id);

        if ($proyect_id != "") {

            $this->paginate = array('Follow' => array(
                    'maxLimit' => 500,
                    'limit' => 50,
                    'recursive' => -1,
                    'fields' => array('Follow.observaciones', 'Follow.id'),
                    'joins' => array(
                        array('table' => 'final_evaluations', 'alias' => 'FinalEvaluation', 'type' => 'left', 'conditions' => 'FinalEvaluation.id=Follow.final_evaluation_id'),
                    )
                    ));
            $this->set('Follows', $this->paginate(array('FinalEvaluation.proyect_id' => $proyect_id)));
        } else {
            $this->Session->setFlash('No ha seleccionado proyecto', 'flash_custom');
            $this->redirect(array('controller' => 'Pages', 'action' => 'display'));
        }
    }

    public function print_letter($follow_id) {
        $this->layout = "ajax";
        $proyect_id = $this->Session->read('proy_id');
        $this->set('proyect_id', $proyect_id);
        $this->set('follow_id', $follow_id);

        if ($proyect_id != "") {

            $this->set('comites', $this->Follow->Committee->find('all', array('recursive' => -1, 'conditions' => array('Committee.follow_id' => $follow_id))));
        } else {
            $this->Session->setFlash('No ha seleccionado proyecto', 'flash_custom');
            $this->redirect(array('controller' => 'Pages', 'action' => 'display'));
        }
    }

    public function delete($id) {
        if ($this->Follow->delete($id)) {
            $this->Session->setFlash('Registro borrado correctamente');
            $this->redirect(array('controller' => 'Follows', 'action' => 'index'));
        } else {
            $this->Session->setFlash('Error borrando los datos, intentelo nuevamente.');
        }
    }

    public function close($follow_id) {

        $this->Follow->id = $follow_id;
        if ($this->Follow->field('cerrado', array('Follow.id' => $follow_id)) == 1) {
            if ($this->Follow->saveField('cerrado', 0)) {
                $this->loadModel('Budget');
                $this->Budget->updateAll(array('Budget.cerrado' => 0), array('Budget.follow_id' => $follow_id));
                $this->Session->setFlash('Registro abierto correctamente', 'flash_custom');
                $this->redirect(array('controller' => 'Follows', 'action' => 'index'));
            }
        } else {
            if ($this->Follow->saveField('cerrado', 1)) {
                $this->loadModel('Budget');
                $this->Budget->updateAll(array('Budget.cerrado' => 1), array('Budget.follow_id' => $follow_id));
                $this->Session->setFlash('Registro cerrado correctamente', 'flash_custom');
                $this->redirect(array('controller' => 'Follows', 'action' => 'index'));
            }
        }
    }

}

?>